using System;
using System.IO;
using UnityEditor;
using UnityEngine;

namespace XFramework
{
    [CreateAssetMenu(fileName = "Setting", menuName = "CodeGen/CodeGenSetting", order = 1)]
    public class CodeGenSetting : ScriptableObject
    {
        public string Namespace = string.Empty;
        public string BaseName = "UIBase";
        public string ScriptDir = "Assets/Scripts/Game";
        public string PrefabDir = "Assets/Art/Prefab";

        private static readonly Lazy<string> Dir = new Lazy<string>(() => "Assets/CodeGen/");
        private const string FileName = "Setting.asset";

        private static CodeGenSetting I;

        public static CodeGenSetting Load()
        {
            if (null != I) return I;

            var filePath = Dir.Value + FileName;
            if (File.Exists(filePath))
            {
                return I = AssetDatabase.LoadAssetAtPath<CodeGenSetting>(filePath);
            }

            return I = CreateInstance<CodeGenSetting>();
        }

        public void Save()
        {
            var filePath = Dir.Value + FileName;
            if (!File.Exists(filePath))
            {
                AssetDatabase.CreateAsset(this, Dir.Value + FileName);
            }

            EditorUtility.SetDirty(this);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }
    }
}